package org.example.easybuy.controller;

import com.antherd.smcrypto.sm3.Sm3;
import org.example.easybuy.entity.User;
import org.example.easybuy.service.LoginService;
import org.example.easybuy.service.UserService;
import org.example.easybuy.service.impl.EmailService;
import org.example.easybuy.util.CustomResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import java.util.Date;
import java.util.Random;
import java.util.concurrent.TimeUnit;

@RestController
public class RegisterController {
    @Autowired
    private EmailService emailService;

    @Autowired
    private UserService userService;

    @Autowired
    private LoginService loginService;

    @Autowired
    private StringRedisTemplate stringRedisTemplate;

    @GetMapping("/getValidateNum")
    public CustomResult getValidateNum(String email)
    {
        String num="";
        Random random=new Random();
        for(int i=1;i<=6;i++)
        {
            num+=random.nextInt(10);
        }
        System.out.println("------------------验证码"+num);
        if(emailService.sendMail(email,"易买网注册验证码,有效期至5分钟",num))
        {
            stringRedisTemplate.opsForValue().set("validateNum",num,5, TimeUnit.MINUTES);
            return CustomResult.successResult().addResult("isSuccess","yes");
        }
        return CustomResult.successResult().addResult("isSuccess","no");
    }

    @PostMapping("/doRegister")
    public CustomResult doRegister(@RequestBody User user)
    {
        if(!stringRedisTemplate.hasKey("validateNum"))
        {
            return CustomResult.errorResult(500,"error").addResult("validateInfo","no");
        }
        String validateNum=stringRedisTemplate.opsForValue().get("validateNum");
        if(!validateNum.equals(user.getValidateNum()))
        {
            return CustomResult.errorResult(500,"error").addResult("validateInfo","inCorrect");
        }
        stringRedisTemplate.delete("validateNum");
        //加签
        String smPass= Sm3.sm3(user.getPassword());
        user.setPassword(smPass);
        user.setCreateTime(new Date());
        user.setType(0);
        user.setSex(1);
        user.setDelFlag(0);
        if(userService.registerUser(user))
        {
            return CustomResult.successResult().addResult("validateInfo","success");
        }
        return CustomResult.successResult().addResult("validateInfo","fail");
    }

    @GetMapping("/checkLoginName")
    public CustomResult checkLoginName(String loginName)
    {
        User user=loginService.selectUserByLoginName(loginName);
        CustomResult customResult=CustomResult.successResult().addResult("user",user);
        if(user==null)
        {
            return customResult.addResult("isRepeat","no");
        }
        else{
            return customResult.addResult("isRepeat","yes");
        }
    }
}
